package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwbhT;
import cn.e4j.modular.crm.entity.GwbjT;
import cn.e4j.modular.crm.entity.GwbkT;
import cn.e4j.modular.crm.entity.GwblT;
import cn.e4j.modular.crm.service.impl.Gwai700ServiceImpl;
import cn.e4j.modular.crm.service.impl.GwbhTServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 拜访记录维护
 * Created by miaoyl on 2019-4-17.
 */
@Controller
@RequestMapping("/crm/gwat160")
public class Gwat160Controller extends BaseController {

    @Autowired
    private GwbhTServiceImpl gwbhTService;

    @Autowired
    private Gwai700ServiceImpl gwai700Service;

    /**
     * 跳转拜访记录维护管理页面
     */
    @RequiresPermissions("crm:gwat160:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwat160.html";
    }

    /**
     * 查询拜访记录维护信息列表
     */
    @RequestMapping("/getGwbhlist")
    @RequiresPermissions("crm:gwat160:init")
    @ResponseBody
    public ResponseData getGwbhlist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbhTService.getGwbhlist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwat160:add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwbkT> gwbkTList = JSONObject.parseArray(jsonObject.getString("listData1"), GwbkT.class);
        List<GwblT> gwblTList = JSONObject.parseArray(jsonObject.getString("listData2"), GwblT.class);
        List<GwbhT> gwbhTList  = JSONObject.parseArray(jsonObject.getString("listData1"), GwbhT.class);
        gwbhTService.addData(gwbkTList, gwblTList, gwbhTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwat160:edit")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwbkT> gwbkTList = JSONObject.parseArray(jsonObject.getString("listData1"), GwbkT.class);
        List<GwblT> gwblTList = JSONObject.parseArray(jsonObject.getString("listData2"), GwblT.class);
        List<GwbhT> gwbhTList  = JSONObject.parseArray(jsonObject.getString("listData1"), GwbhT.class);
        gwbhTService.editData(gwbkTList, gwblTList, gwbhTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwat160:del")
    @ResponseBody
    public ResponseData del(GwbhT gwbhT) {
        gwbhTService.delete(gwbhT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwat160:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbhT> gwbhTList = inDto.getReqList(GwbhT.class);
        gwbhTService.delDataBatch(gwbhTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updateData")
    @ResponseBody
    public ResponseData updateData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbhT> gwbhTList = inDto.getReqList(GwbhT.class);
        gwbhTService.updateData(gwbhTList);
        return SUCCESS_TIP;
    }

    @RequestMapping("/listGwbf")
    @ResponseBody
    public ResponseData listGwbf(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWBF001",shiroUser.getORG_ID());
        List<Dto> result = gwbhTService.listGwbf(inDto);
        return ResponseData.success(result);
    }

    @RequestMapping("/gwat160Spmx")
    @ResponseBody
    public ResponseData gwat160Spmx(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWBKACTI", "Y");
        inDto.put("GWBKNAME", inDto.getString("GWBHNAME"));
        inDto.put("GWBKID", inDto.getString("GWBHID"));
        List<Dto> result = new ArrayList<>();
        BigDecimal GWBKSEQ2 = gwbhTService.getGwb004(inDto);
        if (GWBKSEQ2 != null) {
            inDto.put("GWBKSEQ2", GWBKSEQ2);
            result = gwbhTService.gwat160Spmx(inDto);
            return ResponseData.success(result);
        }
        return ResponseData.success(result);
    }

    @RequestMapping("/getCustomerData")
    @ResponseBody
    public ResponseData getCustomerData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwai700Service.getCustomerData(inDto.getString("GWBHID"));
        return ResponseData.success(result);
    }

    @RequestMapping("/getGwblData")
    @ResponseBody
    public ResponseData getGwblData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwbhTService.listGwbc(inDto);
        for (int i = 0; i < result.size(); i++) {
            inDto.put("GWBL001", result.get(i).getString("GWBC001"));
            Dto dto = gwbhTService.listGwbl(inDto);
            if (dto == null) {
                result.get(i).put("STATUS", "N");
                result.get(i).put("LAY_CHECKED", false);
            } else {
                result.get(i).put("STATUS", "Y");
                result.get(i).put("LAY_CHECKED", true);
            }
        }
        return ResponseData.success(result);
    }

    @RequestMapping("/getGwbjData")
    @ResponseBody
    public ResponseData getGwbjData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwbhTService.listGwbj(inDto);
        return ResponseData.success(result);
    }

    @RequestMapping("/calc")
    @ResponseBody
    public ResponseData calc(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwbhTService.calc(inDto);
        return ResponseData.success(result);
    }

    @RequestMapping("/getGwcsData")
    @ResponseBody
    public ResponseData getGwcsData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbhTService.listGwcs1(page,inDto);
        return new PageReData(page, result);
    }

    @RequestMapping("/getGwbkData")
    @ResponseBody
    public ResponseData getGwbkData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbhTService.getGwbkData1(page,inDto);
        return new PageReData(page, result);
    }

    @RequestMapping("/getDetailData")
    @ResponseBody
    public ResponseData getDetailData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        List<Dto> list = gwbhTService.getGwbh(inDto);
        List<Dto> gwbf = gwbhTService.listGwbf(inDto);
        for (int i = 0; i < list.size(); i++) {
            String gwbh011 = list.get(i).getString("GWBH011");
            gwbh011 = gwbh011.substring(1, gwbh011.length() - 1);
            String gwbh = gwbh011;
            int g = 0;
            for (int j = 0; j < gwbf.size(); j++) {
                if (gwbf.get(j).getString("GWBF002").indexOf(gwbh) == -1) {
                    gwbh = gwbh.replaceAll(gwbf.get(j).getString("GWBF002"), "[" + gwbf.get(j).getString("GWBFL004") + "]");
                }
            }
            list.get(i).put("GWBH011", gwbh);
            String[] gwbh1 = gwbh.split("a,a");
            List<String> gwbd = Arrays.asList(gwbh1);
            for (int h = 0; h < gwbh1.length; h++) {
                gwbh1[h] = gwbh1[h].replaceAll("a", "");
            }
            list.get(i).put("GWBF", gwbd);
        }
        inDto.put("GWCS010", inDto.getString("GWBHID"));
        Dto result = gwbhTService.calc(inDto);
        if (list.size() > 0) {
            outDto.put("GWAKL004", list.get(0).getString("GWAKL004"));
            outDto.put("GWBGL003", list.get(0).getString("GWBGL003"));
            outDto.put("GWAVL003", list.get(list.size() - 1).getString("GWAVL003"));
            outDto.put("GWBH002", list.get(0).getString("GWBH002"));
            outDto.put("GWBHORIG", list.get(0).getString("GWBHORIG"));
            outDto.put("GWBH031", list.get(list.size() - 1).getString("GWBH031"));
        }
        outDto.put("dto",list);
        outDto.put("GWBHNAME",inDto.getString("GWBHNAME"));
        outDto.put("GWBHID",inDto.getString("GWBHID"));
        outDto.put("GWBHSEQ",inDto.getString("GWBHSEQ"));
        outDto.put("RESULT",result);
        return ResponseData.success(outDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/addGwbj")
    @ResponseBody
    public ResponseData addGwbj(GwbjT gwbjT){
        gwbhTService.addGwbj(gwbjT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/delGwbjData")
    @ResponseBody
    public ResponseData delGwbjData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbjT> gwbjTList = inDto.getReqList(GwbjT.class);
        gwbhTService.delGwbjData(gwbjTList);
        return SUCCESS_TIP;
    }

    @RequestMapping("/getCount")
    @ResponseBody
    public ResponseData getCount(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwbhTService.getCount(inDto);
        return ResponseData.success(result);
    }

    /**
     * 商机编号开窗
     */
    @RequestMapping("/getGwbglist")
    @ResponseBody
    public ResponseData getGwbglist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwbhTService.getGwbglist(page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping("/getGwbh011")
    @ResponseBody
    public ResponseData getGwbh011(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwbhTService.getGwbh011(inDto);
        return ResponseData.success(result);
    }
}
